
//************************************************************************
// Table A2
//************************************************************************

local policy_date=14943							//policy change date in days 
local citation_window=3650						//max priority-priority lag
local window_size=7								//size of time window for repeated cross-sectional analysis
local window_num=26							//number of times windows either side of policy change
local window=`window_num'*`window_size'			//resulting cohort window size						
local country="US"								//select non-JP citing country

global DATA  "path/to/input/data"   
global OUT "path/to/output/folder"
global INT "path/to/intermediate/files"

local controls_fes="i.time science_cites originality i.wipo_cited_broad"

est clear 

foreach set in all conditional{
	foreach reg in ols poisson {
		*take base set
		use "$DATA\twincites_applicant_did_prep_citecounts.dta", clear
		
		if "`set'"=="all" {
			drop if cite_count>8
		}
		
		if "`set'"=="conditional" {
			drop if cite_count==0
			bysort family_cited: egen total=count(family_cited)
			keep if total==2
			drop if cite_count>14
		}
		
		if "`reg'"=="ols" {
			if "`set'"=="all" {
				replace cite_count=ln(cite_count+1)
				quietly: sum cite_count
				replace cite_count=(cite_count)/r(sd)
			}
			if "`set'"=="conditional" {
				replace cite_count=ln(cite_count)
				quietly: sum cite_count
				replace cite_count=(cite_count)/r(sd)
			}
		}
		
		*set up time parameter
		gen temp=floor((US_filing_date_d_cited-`policy_date')/`window_size')
		quietly: sum temp
		gen time=temp-r(min)
		
		display "==================================================="
		display "`reg', `set' "
		display "==================================================="
		if "`reg'"=="ols" eststo: xi: reg cite_count treated nonJP `controls_fes', vce(cluster nonJP)
		if "`reg'"=="ols" esttab using "$OUT\baseline_did_citecount.tex", replace ///
		b(3) p(3) nomtitle nonote label booktabs f star(* 0.10 ** 0.05 *** 0.01) ///
		keep(treated nonJP science_cites originality _cons) ///
		mlabel( "All_ols" "Conditional_ols") indicate("Time FEs = _Itime_*" "Cited field FEs = _Iwipo_cite*") alignment(D{.}{.}{-1})
	
	if "`reg'"=="ols" didregress (cite_count  `controls_fes') (treated), group(nonJP) time(time) 
	if "`reg'"=="ols" estat ptrends	
	
	if "`reg'"=="poisson" eststo: xi: poisson cite_count treated nonJP `controls_fes', vce(cluster nonJP)
		if "`reg'"=="poisson" esttab using "$OUT\baseline_did_citecount_poisson.tex", replace ///
		b(3) p(3) nomtitle nonote label booktabs f star(* 0.10 ** 0.05 *** 0.01) ///
		keep(treated nonJP science_cites originality _cons) ///
		mlabel( "All_poisson" "Conditional_poisson") indicate("Time FEs = _Itime_*" "Cited field FEs = _Iwipo_cite*") alignment(D{.}{.}{-1})
	

	
	}
} 

